<template>
  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <div class="site-content">
        <div class="brand-info">
          <h2 class="brand-info__text">东北大学在线图书信息管理系统</h2>
          <p class="brand-info__intro">© 东北大学版权所有</p>
        </div>
        <div class="login-main">
          <h3 class="login-title">管理员登录</h3>
          <br />
          <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" @keyup.enter.native="onSubmit()" status-icon>
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" placeholder="帐号" prefix-icon="el-icon-user-solid"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="el-icon-star-on"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn-submit" type="primary" @click="onSubmit()">登录</el-button>
              <el-button class="login-btn-submit" type="info" @click="reset()">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script> 
export default {
  data() {
    return {
      loginForm: {
        username: "", password: "",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    }
  },
  created() {
  },
  methods: {
    onSubmit() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          const formData = new FormData();
          formData.append("username", this.loginForm.username);
          formData.append("password", this.loginForm.password);
          const _this = this;
          this.$http
            .post("/api/login", formData)
            .then((res) => {
              if (res.code == 0) {
                _this.$store.commit("set_token", res.data);
                _this.$message.success("登录成功");
                const vue = this;
                vue.$router.push("/home");
                _this.loadDic();
              } else {
                _this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              console.log(err.message);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    loadDic() {
      const _this = this;
      this.$http
        .get("/api/sysDic/all-list", {})
        .then((res) => {
          if (res.code == 0) {
            _this.$store.commit("set_dicset", res.data);
          }
        })
        .catch((err) => { });
    },
    reset() {
      this.$refs.loginFormRef.resetFields();
    },
  }
}
</script>

<style lang="less">
.site-wrapper.site-page--login {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(38, 50, 56, 0.6);
  overflow: hidden;
  &:before {
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
    width: 100%;
    height: 100%;
    content: "";
    background-image: url(~@/assets/img/login_bg.jpg);
    background-size: cover;
  }
  .site-content__wrapper {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 0;
    margin: 0;
    overflow-x: hidden;
    overflow-y: hidden;
    background-color: transparent;
  }
  .site-content {
    min-height: 100%;
    padding: 30px 500px 30px 30px;
  }
  .brand-info {
    margin: 220px 100px 0 90px;
    color: #fff;
  }
  .brand-info__text {
    margin: 0 0 22px 0;
    font-size: 48px;
    font-weight: 400;
    text-transform: uppercase;
  }
  .brand-info__intro {
    margin: 10px 0;
    font-size: 16px;
    line-height: 1.58;
    opacity: 0.6;
  }
  .login-main {
    position: absolute;
    top: 0;
    right: 0;
    padding: 150px 60px 180px;
    width: 400px;
    min-height: 100%;
    background-color: #fff;
    input {
      height: 40px;
    }
    button {
      height: 40px;
    }
  }
  .login-title {
    font-size: 20px;
    text-align: left;
  }
  .login-captcha {
    overflow: hidden;
    > img {
      width: 100%;
      cursor: pointer;
    }
  }
  .login-btn-submit {
    width: 47%;
    margin-top: 38px;
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
